package cn.droidlover.xrecyclerview.demo.slice;


import cn.droidlover.xrecyclerview.XRecyclerView;
import cn.droidlover.xrecyclerview.demo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.multimodalinput.event.TouchEvent;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice  {
    private XRecyclerView listContainer;
    private ArrayList<String> data;
    private SampleItemProvider provider;
    private AnimatorProperty mAnimator,mAnimator2;
    private Image mTopImage,mBottomImage;
    private  DirectionalLayout  directionalLayout;
    private int page=1;
    private final EventHandler handler = new EventHandler(EventRunner.getMainEventRunner());
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mTopImage = (Image) findComponentById(ResourceTable.Id_image);
        mAnimator = mTopImage.createAnimatorProperty();
        mBottomImage = (Image) findComponentById(ResourceTable.Id_image1);
        mAnimator2 = mBottomImage.createAnimatorProperty();
        directionalLayout=(DirectionalLayout)findComponentById(ResourceTable.Id_layoutImage);
        findComponentById(ResourceTable.Id_next).setClickedListener(component -> present(new NextAbilitySlice(), new Intent()));
        listContainer = (XRecyclerView) findComponentById(ResourceTable.Id_list_container);
        listContainer.setLongClickable(false);
        listContainer.setReboundEffect(false);
        addContentComponent();
        listContainer.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                handler.removeTask(refreshRunnable);
                handler.postTask(refreshRunnable, 1000);
                startTopLoading();
                new EventHandler(EventRunner.getMainEventRunner()).postTask(() -> {
                    data.clear();
                    page=1;
                    getData(page);
                    provider.notifyDataChanged();
                },1000);
            }

            @Override
            public void onLoadMore() {
                if(data.size()==15){
                    return ;
                }else {
                    startBottomLoading();
                }
                handler.postTask(refreshRunnable, 1000);
                new EventHandler(EventRunner.getMainEventRunner()).postTask(() -> {
                    int size=data.size();
                    page++;
                    getData(page);
                    provider.notifyDataSetItemRangeChanged(size,data.size());

                },1000);
            }
        });

    }
    //开始下拉动画
    private void startTopLoading() {
        mTopImage.setVisibility(Component.VISIBLE);
        mAnimator.setDuration(600).rotate(360).setLoopedCount(AnimatorProperty.INFINITE).setTarget(mTopImage).start();

    }

    //关闭下拉动画
    private void stopTopLoading() {
        mTopImage.setVisibility(Component.HIDE);
        mAnimator.cancel();
    }
    //开始动画
    private void startBottomLoading() {
        directionalLayout.setVisibility(Component.VISIBLE);
        mAnimator2.setDuration(600).rotate(360).setLoopedCount(AnimatorProperty.INFINITE).setTarget(mBottomImage).start();

    }

    //关闭上拉动画
    private void stopBottomLoading() {
        directionalLayout.setVisibility(Component.HIDE);
        mAnimator2.cancel();
    }

    //添加添加数据到listContain
    private void addContentComponent() {
        data = new ArrayList<>();
        provider = new SampleItemProvider(this,getData(1) );
        listContainer.setItemProvider(provider);
    }

    private List<String> getData( int page) {

        if (data.size()< 17) {
            if(page==1){
                for (int pos = 0; pos < 3; pos++) {
                    data.add("测试" + pos);
                }
            }else{
                for (int pos = data.size(); pos < page*3; pos++) {
                    data.add("测试" + pos);
                }
            }

        }


        return data;
    }

    @Override
    public void onActive() {
        super.onActive();

    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
    private final Runnable refreshRunnable = () -> {
        stopTopLoading();
        stopBottomLoading();
    };

}
